package com.eims.sls_app.ui.adapter;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.contans.view.HallDanduanInfoVo;
import com.eims.sls_app.utils.ImageManager;
import com.eims.sls_app.view.DanguanIsShowVo;
import com.listener.OnHallDanguanpositionPaySureListener;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Spannable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import bls.juejin.cn.R;

public class PageAdapter extends BaseAdapter {
	private List<HallDanduanInfoVo> list;
	LayoutInflater inflater;
	private Context context;
	private boolean ischoice_01;
	private boolean ischoice_02;
	private boolean ischoice_03;
	private DecimalFormat format = new DecimalFormat("#####0.00");

	public List<DanguanIsShowVo> danguanIsShowVos = new ArrayList<DanguanIsShowVo>();
	private OnHallDanguanpositionPaySureListener sureListener;
	public int numChoice;
	public double allMoney;
	public String strJJMoney;

	public PageAdapter(Context context, List<HallDanduanInfoVo> list,
			OnHallDanguanpositionPaySureListener sureListener) {
		this.list = list;
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.sureListener = sureListener;
		danguanIsShowVos.clear();
		
		if (list.size() > 0) {

			for (int i = 0; i < list.size(); i++) {
				ischoice_01 = false;
				ischoice_02 = false;
				ischoice_03 = false;
				if (list.get(i).getSpf().equals("3")) {

					ischoice_01 = true;

				}
				if (list.get(i).getSpf().equals("1")) {

					ischoice_02 = true;

				}
				if (list.get(i).getSpf().equals("0")) {

					ischoice_03 = true;

				}
				DanguanIsShowVo danguanIsShowVo = new DanguanIsShowVo();
				danguanIsShowVo.setIschoice_01(ischoice_01);
				danguanIsShowVo.setIschoice_02(ischoice_02);
				danguanIsShowVo.setIschoice_03(ischoice_03);
				danguanIsShowVos.add(danguanIsShowVo);

			}
		}
		System.out.println("-----创建page------"+danguanIsShowVos.toString());
	}

	@Override
	public int getCount() {

		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CacheView cacheView;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_page, null);
			cacheView = new CacheView();
			cacheView.btnPayOk = (TextView) convertView.findViewById(R.id.hall_lr_danguan_btn_pay);
			cacheView.tvStopTime = (TextView) convertView.findViewById(R.id.hall_lr_danguan_tv_time);
			cacheView.tv01 = (TextView) convertView.findViewById(R.id.tv_01);
			cacheView.tvmainTeam = (TextView) convertView.findViewById(R.id.rl_duiwu_01_tv_duiwu);
			cacheView.tvguestTeam = (TextView) convertView.findViewById(R.id.rl_duiwu_03_tv_duiwu);
			cacheView.tvspfwin = (TextView) convertView.findViewById(R.id.rl_duiwu_01_tv_peilv);
			cacheView.tvspfflat = (TextView) convertView.findViewById(R.id.rl_duiwu_02_tv_peilv);
			cacheView.tvspflose = (TextView) convertView.findViewById(R.id.rl_duiwu_03_tv_peilv);
			cacheView.tvzs = (TextView) convertView.findViewById(R.id.hall_lr_danguan_tv_tuijian_num);

			cacheView.ivMainTeam = (ImageView) convertView.findViewById(R.id.hall_rl_tv_gaunahu_thui_01);
			cacheView.ivGuestTeam = (ImageView) convertView.findViewById(R.id.hall_rl_tv_gaunahu_thui_02);
			cacheView.ivTuijian_01 = (ImageView) convertView.findViewById(R.id.hall_lr_danguan_iv_01);
			cacheView.ivTuijian_02 = (ImageView) convertView.findViewById(R.id.hall_lr_danguan_iv_02);
			cacheView.ivTuijian_03 = (ImageView) convertView.findViewById(R.id.hall_lr_danguan_iv_03);
			cacheView.ivTuijian_04 = (ImageView) convertView.findViewById(R.id.hall_lr_danguan_iv_04);
			cacheView.ivTuijian_05 = (ImageView) convertView.findViewById(R.id.hall_lr_danguan_iv_05);

			cacheView.ivDuijian_01 = (ImageView) convertView.findViewById(R.id.rl_duiwu_01_iv_duiwu);
			cacheView.ivDuijian_02 = (ImageView) convertView.findViewById(R.id.rl_duiwu_02_iv_duiwu);
			cacheView.ivDuijian_03 = (ImageView) convertView.findViewById(R.id.rl_duiwu_03_iv_duiwu);
			cacheView.rlChoice_01 = (RelativeLayout) convertView.findViewById(R.id.rl_duiwu_01);
			cacheView.rlChoice_02 = (RelativeLayout) convertView.findViewById(R.id.rl_duiwu_02);
			cacheView.rlChoice_03 = (RelativeLayout) convertView.findViewById(R.id.rl_duiwu_03);

			cacheView.spannable = (TextView) convertView.findViewById(R.id.contentTextView);
			cacheView.tvJJ = (TextView) convertView.findViewById(R.id.tv_jj);

			cacheView.rlChoice_01.setTag(position);
			cacheView.rlChoice_02.setTag(position);
			cacheView.rlChoice_03.setTag(position);
			cacheView.btnPayOk.setTag(position);

			// cacheView.imgv_img=(ImageView) convertView.findViewById(R.id.imageView1);
			convertView.setTag(cacheView);
		} else {
			cacheView = (CacheView) convertView.getTag();
		}

		try {







			cacheView.ivDuijian_01.setVisibility(View.GONE);
			cacheView.ivDuijian_02.setVisibility(View.GONE);
			cacheView.ivDuijian_03.setVisibility(View.GONE);

//			cacheView.tvmainTeam.setTextColor(context.getResources().getColor(R.color.hall_danguan_team_bg));
//			cacheView.tvguestTeam.setTextColor(context.getResources().getColor(R.color.hall_danguan_team_bg));
			cacheView.tvspfwin.setTextColor(context.getResources().getColor(R.color.hall_danguan_peiiv_bg));
			cacheView.tvspfflat.setTextColor(context.getResources().getColor(R.color.hall_danguan_peiiv_bg));
			cacheView.tvspflose.setTextColor(context.getResources().getColor(R.color.hall_danguan_peiiv_bg));

			cacheView.rlChoice_01.setBackgroundResource(R.drawable.lr_danguan_input_border);
			cacheView.rlChoice_02.setBackgroundResource(R.drawable.lr_danguan_input_border);
			cacheView.rlChoice_03.setBackgroundResource(R.drawable.lr_danguan_input_border);
//			cacheView.tvStopTime.setText();

			SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date=simpleDateFormat.parse(list.get(position).getStoptime());
			SimpleDateFormat dateFm = new SimpleDateFormat("EEEE HH:mm");
			String currSun = dateFm.format(date);
			cacheView.tvStopTime.setText(currSun+" 截止");


			cacheView.tvmainTeam.setText(list.get(position).getMainTeam());
			cacheView.tvguestTeam.setText(list.get(position).getGuestTeam());


			cacheView.tvspfwin.setText("胜 " + list.get(position).getSpfwin());
			cacheView.tvspfflat.setText("平 " + list.get(position).getSpfflat());
			cacheView.tvspflose.setText("负 " + list.get(position).getSpflose());
			cacheView.tvzs.setText(list.get(position).getZs());

//			ImageManager.getInstance().displayImage(
//					"http://888.qcai888.cn/Images/Team/" + removeAllSpace(list.get(position).getMainTeam()) + ".png",
//					cacheView.ivMainTeam, ImageManager.getViewsHeadOptions());
//			ImageManager.getInstance()
//					.displayImage("http://888.qcai888.cn/Images/Team/"
//							+ removeAllSpace(list.get(position).getGuestTeam()) + ".png", cacheView.ivGuestTeam,
//							ImageManager.getViewsHeadOptions());

			if (list.get(position).getSpf().equals("3")) {
				cacheView.ivDuijian_01.setVisibility(View.VISIBLE);

			} else {
				cacheView.ivDuijian_01.setVisibility(View.GONE);
			}
			if (list.get(position).getSpf().equals("1")) {
				cacheView.ivDuijian_02.setVisibility(View.VISIBLE);

			} else {
				cacheView.ivDuijian_02.setVisibility(View.GONE);
			}
			if (list.get(position).getSpf().equals("0")) {
				cacheView.ivDuijian_03.setVisibility(View.VISIBLE);

			} else {
				cacheView.ivDuijian_03.setVisibility(View.GONE);
			}
			if (danguanIsShowVos.get(position).isIschoice_01()) {

				cacheView.rlChoice_01.setBackgroundResource(R.drawable.lr_danguan_input_border_on);
				strJJMoney = 200 * Double.parseDouble(list.get(position).getSpfwin()) + "";
//				cacheView.tvmainTeam.setTextColor(context.getResources().getColor(R.color.white));
				cacheView.tvspfwin.setTextColor(context.getResources().getColor(R.color.white));
			}
			if (danguanIsShowVos.get(position).isIschoice_02()) {

				cacheView.rlChoice_02.setBackgroundResource(R.drawable.lr_danguan_input_border_on);
				strJJMoney = 200 * Double.parseDouble(list.get(position).getSpfflat()) + "";
				cacheView.tvspfflat.setTextColor(context.getResources().getColor(R.color.white));
			}
			if (danguanIsShowVos.get(position).isIschoice_03()) {
				cacheView.rlChoice_03.setBackgroundResource(R.drawable.lr_danguan_input_border_on);

				strJJMoney = 200 * Double.parseDouble(list.get(position).getSpflose()) + "";
//				cacheView.tvguestTeam.setTextColor(context.getResources().getColor(R.color.white));
				cacheView.tvspflose.setTextColor(context.getResources().getColor(R.color.white));
			}

			if (list.get(position).getZs().equals("0")) {
				cacheView.ivTuijian_01.setBackgroundResource(R.drawable.my_start);
				cacheView.ivTuijian_02.setBackgroundResource(R.drawable.my_start);
				cacheView.ivTuijian_03.setBackgroundResource(R.drawable.my_start);
				cacheView.ivTuijian_04.setBackgroundResource(R.drawable.my_start);
				cacheView.ivTuijian_05.setBackgroundResource(R.drawable.my_start);
			} else if (list.get(position).getZs().equals("1")) {
				cacheView.ivTuijian_01.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_02.setBackgroundResource(R.drawable.my_start);
				cacheView.ivTuijian_03.setBackgroundResource(R.drawable.my_start);
				cacheView.ivTuijian_04.setBackgroundResource(R.drawable.my_start);
				cacheView.ivTuijian_05.setBackgroundResource(R.drawable.my_start);
			} else if (list.get(position).getZs().equals("2")) {
				cacheView.ivTuijian_01.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_02.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_03.setBackgroundResource(R.drawable.my_start);
				cacheView.ivTuijian_04.setBackgroundResource(R.drawable.my_start);
				cacheView.ivTuijian_05.setBackgroundResource(R.drawable.my_start);
			} else if (list.get(position).getZs().equals("3")) {
				cacheView.ivTuijian_01.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_02.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_03.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_04.setBackgroundResource(R.drawable.my_start);
				cacheView.ivTuijian_05.setBackgroundResource(R.drawable.my_start);
			} else if (list.get(position).getZs().equals("4")) {
				cacheView.ivTuijian_01.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_02.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_03.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_04.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_05.setBackgroundResource(R.drawable.my_start);
			} else if (list.get(position).getZs().equals("5")) {
				cacheView.ivTuijian_01.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_02.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_03.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_04.setBackgroundResource(R.drawable.my_start_on);
				cacheView.ivTuijian_05.setBackgroundResource(R.drawable.my_start_on);
			}

//			cacheView.btnPayOk.setText(GetYujiJJMoney(danguanIsShowVos.get(position).isIschoice_01(),
//					danguanIsShowVos.get(position).isIschoice_02(), danguanIsShowVos.get(position).isIschoice_03(),
//					position));

	String info=GetYujiJJMoney(danguanIsShowVos.get(position).isIschoice_01(),
					danguanIsShowVos.get(position).isIschoice_02(), danguanIsShowVos.get(position).isIschoice_03(),
					position);

cacheView.spannable.setText(info);
cacheView.tvJJ.setText(strJJMoney);

			cacheView.rlChoice_01.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					RelativeLayout relativeLayout = (RelativeLayout) v;
					Integer integer = (Integer) relativeLayout.getTag();

					danguanIsShowVos.get(integer).setIschoice_01(!danguanIsShowVos.get(integer).isIschoice_01());
					notifyDataSetChanged();
					System.out.println(danguanIsShowVos.toString());

				}
			});
			cacheView.rlChoice_02.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					RelativeLayout relativeLayout = (RelativeLayout) v;
					Integer integer = (Integer) relativeLayout.getTag();
					// TODO Auto-generated method stub
					danguanIsShowVos.get(integer).setIschoice_02(!danguanIsShowVos.get(integer).isIschoice_02());
					notifyDataSetChanged();
					System.out.println(danguanIsShowVos.toString());
				}
			});
			cacheView.rlChoice_03.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					RelativeLayout relativeLayout = (RelativeLayout) v;
					Integer integer = (Integer) relativeLayout.getTag();
					// TODO Auto-generated method stub
					danguanIsShowVos.get(integer).setIschoice_03(!danguanIsShowVos.get(integer).isIschoice_03());
					notifyDataSetChanged();
					System.out.println(danguanIsShowVos.toString());
				}
			});
			cacheView.btnPayOk.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					TextView relativeLayout = (TextView) v;
					Integer integer = (Integer) relativeLayout.getTag();
					sureListener.onSureClick(danguanIsShowVos.get(integer).isIschoice_01(),
							danguanIsShowVos.get(integer).isIschoice_02(),
							danguanIsShowVos.get(integer).isIschoice_03(), integer,
							danguanIsShowVos.get(integer).getNum(),
							danguanIsShowVos.get(integer).getPayyyjj());

				}
			});

			// btnPayOk.setText("投10元变" + strJJMoney + "元");
		} catch (Exception e) {
			// TODO: handle exception
		}
		return convertView;
	}

	public String GetYujiJJMoney(boolean ischoice_01, boolean ischoice_02, boolean ischoice_03, int position) {
		allMoney = 0.0;
		numChoice = 0;

		String info = "";
		if (ischoice_01) {
			if (allMoney >= (200 * Double.parseDouble(list.get(position).getSpfwin()))) {

			} else {
				allMoney = 200 * Double.parseDouble(list.get(position).getSpfwin());

			}

			numChoice = numChoice + 1;

		}
		if (ischoice_02) {
			if (allMoney >= (200 * Double.parseDouble(list.get(position).getSpfflat()))) {

			} else {
				allMoney = 200 * Double.parseDouble(list.get(position).getSpfflat());
			}
			numChoice = numChoice + 1;
		}
		if (ischoice_03) {
			if (allMoney >= (200 * Double.parseDouble(list.get(position).getSpflose()))) {

			} else {
				allMoney = 200 * Double.parseDouble(list.get(position).getSpflose());
				Log.e("xx", Double.parseDouble(list.get(position).getSpfwin()) + "");
			}

			numChoice = numChoice + 1;
		}

		if (numChoice == 0) {

			info = "0";
		} else if (numChoice == 1) {
			info = "200";
		} else if (numChoice == 2) {
			info = "400";
		} else if (numChoice == 3) {
			info = "600";
		}

		strJJMoney = formateRate(allMoney + "");
		danguanIsShowVos.get(position).setPayyyjj(strJJMoney);
		danguanIsShowVos.get(position).setNum(numChoice);
		return info;
	}

	public String formateRate(String rateStr) {
		if (rateStr.indexOf(".") != -1) {
			// 获取小数点的位置
			int num = 0;
			num = rateStr.indexOf(".");

			// 获取小数点后面的数字 是否有两位 不足两位补足两位
			String dianAfter = rateStr.substring(0, num + 1);
			String afterData = rateStr.replace(dianAfter, "");
			if (afterData.length() < 2) {
				afterData = afterData + "0";
			} else {
				afterData = afterData;
			}
			return rateStr.substring(0, num) + "." + afterData.substring(0, 2);
		} else {
			if (rateStr == "1") {
				return "100";
			} else {
				return rateStr;
			}
		}
	}

	public String removeAllSpace(String str) {
		String tmpstr = str.replace(" ", "");
		return tmpstr;
	}

	private static class CacheView {
		private TextView  tv01,tvStopTime, tvmainTeam, tvguestTeam, tvspfwin, tvspfflat, tvspflose, tvzs;
		private ImageView ivMainTeam, ivGuestTeam, ivTuijian_01, ivTuijian_02, ivTuijian_03, ivTuijian_04, ivTuijian_05;
		private ImageView ivDuijian_01, ivDuijian_02, ivDuijian_03;
		private RelativeLayout rlChoice_01, rlChoice_02, rlChoice_03;
		private TextView btnPayOk;
		private TextView  spannable,tvJJ;
		// ImageView imgv_img;
	}
}